package com.minhang.adsb.service.impl;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.minhang.adsb.constant.JsonEnum;
import com.minhang.adsb.entity.Airlines;
import com.minhang.adsb.entity.ServerConfig;
import com.minhang.adsb.model.AdsbPoint;
import com.minhang.adsb.model.AirTrackData;
import com.minhang.adsb.service.PushStrategy;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
@Slf4j
public class V2ProcessedHttpStrategy implements PushStrategy {

    @Autowired
    @Qualifier("airlinesRedisTemplate")
    private RedisTemplate<String, Airlines> redisTemplate;
    private final V1HttpPushStrategy v1Strategy;

    public V2ProcessedHttpStrategy(V1HttpPushStrategy v1Strategy) {
        this.v1Strategy = v1Strategy;
    }
    @Override
    public void push(ServerConfig server, String jsonPayload) {
        // V2版本处理数据后推送
        String processedData = processData(server,jsonPayload);
        v1Strategy.push(server, processedData);
    }

    private String processData(ServerConfig serverConfig,String rawData) {
        ObjectMapper mapper = JsonEnum.getMapper();
        // 实现数据处理逻辑
        try(JsonParser parser = mapper.getFactory().createParser(rawData)) {
            //读取为list AirTrackData
            Iterator<AirTrackData> iterator = parser.readValuesAs(new TypeReference<AirTrackData>(){});
            List<AdsbPoint> re = new ArrayList<>();
            //todo 处理三字码
            List<Airlines> airlines = redisTemplate.opsForList().range("AIRLINES", 0, -1);
            while (iterator.hasNext()) {
                AirTrackData airTrackDatum = iterator.next();
                if (airTrackDatum.getLat() == null || airTrackDatum.getLon() == null) {
                    // 如果经纬度字段为空，则跳过当前数据
                    continue;
                }
                //判断经纬度存入re
                if (airTrackDatum.getLat() < serverConfig.getMaxLat() && airTrackDatum.getLat() > serverConfig.getMinLat() &&
                        airTrackDatum.getLon() < serverConfig.getMaxLng() && airTrackDatum.getLon() > serverConfig.getMinLng()) {
                    re.add(processAirTrackData2AdsbPoint(airTrackDatum));
                }
            }
            return mapper.writeValueAsString(re);
        } catch (IOException e) {
            throw new RuntimeException("数据处理失败", e);
        }
    }

    private AdsbPoint processAirTrackData2AdsbPoint(AirTrackData airTrackData) {
        AdsbPoint adsbPoint = new AdsbPoint();

        return new AdsbPoint();
    }
}
